home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / m2 / m2_part1.lha / modula / src / StdLib.def < prev    next >
Text File  |  1994-07-30  |  6KB  |  157 lines

  1. DEFINITION FOR C MODULE StdLib ;
  2.  
  3. FROM SYSTEM IMPORT ADDRESS, STRING, BYTE ;
  4.  
  5. TYPE
  6.   STRINGPtr = POINTER TO STRING ;
  7.  
  8. CONST
  9.   EXIT_FAILURE    = 1 ;
  10.   EXIT_SUCCESS    = 0 ;
  11.  
  12.   ERANGE    = 2 ;
  13.  
  14.   RAND_MAX    = MAX( LONGINT ) ;
  15.  
  16.   LONG_MAX    = 07FFFFFFFH ;
  17.   LONG_MIN    = 080000000H ;
  18.  
  19.   HUGE_VAL    = 1.0E300 ;
  20.  
  21. VAR
  22.   errno : LONGINT ; (* duplicated from errno.def *)
  23.     (* set this to zero before calling a function and test it afterwards *)
  24.     (* eg. errno := 0 ;                             *)
  25.     (*       x := atof( str ) ;                          *)
  26.     (*     IF errno # 0 THEN (* handle error *) END              *)
  27.  
  28. PROCEDURE abort( ) ;
  29. (* aborts the program, as exit(20) *)
  30.  
  31. PROCEDURE abs( int : LONGINT ) : LONGINT ;
  32. PROCEDURE labs( long : LONGINT ) : LONGINT ;
  33.  
  34. PROCEDURE exit( rc : LONGINT ) ;
  35. (* exits the program with return code rc *)
  36.  
  37. PROCEDURE atexit( p : PROC ) : LONGINT ;
  38. (* At program exit p will be called.                    *)
  39. (* This is normally a procedure that frees any aquired resources etc    *)
  40. (* atexit returns non-zero if p could not be installed.            *)
  41.  
  42. PROCEDURE atoi( cs : STRING ) : LONGINT  ;
  43. (* converts string cs to longint , as strtol( cs , NIL , 10 ) *)
  44.  
  45. PROCEDURE atol( cs : STRING ) : LONGINT  ;
  46. (* as atoi *)
  47.  
  48. PROCEDURE atof( cs : STRING ) : LONGREAL ;
  49. (* converts cs to a longreal , same as strtod( cs , NIL ) *)
  50.  
  51. PROCEDURE strtod( cs : STRING ; endp : STRINGPtr ) : LONGREAL ;
  52. (* converts cs to a longreal , leading spaces are ignored        *)
  53. (* If the conversion will overflow then (+/-)HUGE_VAL is returned.    *)
  54. (* Elsif the result will underflow then 0 is returned.            *)
  55. (* In both cases errno is set to ERANGE.                *)
  56. (* If endp # NIL then endp^ points to any unconverted suffix of cs    *)
  57.  
  58. PROCEDURE strtol( cs : STRING ; endp : STRINGPtr ; base : LONGINT ) : LONGINT ;
  59. (* converts cs to a longint , leading spaces are ignored.           *)
  60. (* Base can be 0 or a number between 2 & 36.                   *)
  61. (* If base = 0 then it is assumed 8 if cs start with a 0 , 16 if cs starts *)
  62. (*  0x or 0X , base 10 otherwise.                       *)
  63. (* If base = 16 the cs can start with 0x or 0X                   *)
  64. (* If the conversion will overflow then LONG_MAX or LONG_MIN is returned   *)
  65. (* In both cases errno is set to ERANGE.                   *)
  66. (* If endp # NIL then endp^ points to any unconverted suffix of cs       *)
  67.  
  68. PROCEDURE getenv( ev : STRING ) : STRING ;
  69. (* getenv returns the environment variable associated with ev        *)
  70. (* (environment variables are set using the amigaDOS SETENV command)    *)
  71. (* or NIL if ev could not be found.                    *)
  72.  
  73. PROCEDURE malloc( size : LONGINT ) : ADDRESS ;
  74. (* Allocates and returns size bytes of uninitialized heap storage *)
  75. (* malloc returns NIL if the memory could not be found.          *)
  76.  
  77. PROCEDURE calloc( nobj : LONGINT ; size : LONGINT ) : ADDRESS ;
  78. (* Allocates and returns an array of nobj elements whose    *)
  79. (* element_size = size bytes. The memory is initialized to 0's.    *)
  80. (* calloc returns NIL if the memory could not be found.        *)
  81.  
  82. PROCEDURE realloc( adr : ADDRESS ; size : LONGINT ) : ADDRESS ;
  83. (* reallocates memory from a previous call to malloc, calloc or realloc         *)
  84. (* The old contents of the old area is copied to the new area,             *)
  85. (* if size is smaller than the previous size then only size bytes are copied *)
  86. (* otherwise the extra area is unitialized. realloc returns NIL if it fails  *)
  87. (* to reallocate the memory.                             *)
  88.  
  89. PROCEDURE free( adr : ADDRESS ) ;
  90. (* frees memory allocated from one of above 3 functions. adr may be NIL *)
  91.  
  92. PROCEDURE rand( ) : LONGINT ;
  93. (* returns a psuedo random number in the range 0 to RAND_MAX *)
  94.  
  95. PROCEDURE srand( seed : LONGINT ) ;
  96. (* reseeds rand.The initial seed is 1 *)
  97.  
  98. PROCEDURE system( cs : STRING ) : LONGINT ;
  99. (* passes cs to amigaDOS for execution, the returned value may or may not *)
  100. (* be the return value of the command (depending on which version of      *)
  101. (* amigaDOS is in use ,V36 should return something useful)          *)
  102.  
  103. TYPE
  104.   CompProc = PROCEDURE( ADDRESS, ADDRESS ) : LONGINT ;
  105. (* bsearch & qsort expect a procedure parameter that returns a -ve result *)
  106. (* if the first arg is greater than the second , 0 is both are same ,     *)
  107. (*  a +ve result otherwise.                          *)
  108.  
  109. PROCEDURE bsearch( VAR key   : ARRAY OF BYTE ;
  110.            VAR base  : ARRAY OF BYTE ;
  111.            num, size : LONGINT ;
  112.            cmpProc   : CompProc ) : ADDRESS ;
  113.  
  114. (* Does a binary chop lookup of key in base, num = number of elements in base *)
  115. (* size = size of key & size of each element in base.                  *)
  116. (* Elements in base must be in ascending order.                          *)
  117. (* bsearch returns NIL if the key could not be matched otherwise a pointer to *)
  118. (* to the matching element.                              *)
  119. (* The address of key is passed as the first parameter to cmpProc.          *)
  120.  
  121. PROCEDURE qsort( VAR base    : ARRAY OF BYTE ;
  122.              n, size : LONGINT ;
  123.              cmp     : CompProc ) ;
  124. (* Sorts base into ascending order *)
  125.  
  126. (* Non-ANSI functions *)
  127.  
  128. TYPE
  129.   IntProc = PROCEDURE( ) : LONGINT ;
  130.  
  131. PROCEDURE onbreak( handler : IntProc ) : IntProc ;
  132. (* handler is called if chkabort detects a break signal, returns the          *)
  133. (* old break handler.                                  *)
  134. (* If the handler returns 0 then the program continues execution otherwise    *)
  135. (* the program exits.The default DICE handler terminates program execution.   *)
  136.  
  137. PROCEDURE chkabort( ) ;
  138. (* Checks to see if the program has been sent a break signal eg ^C pressed.   *)
  139. (* If a break is detected then the the break handler is called.              *)
  140. (* Note some library functions (in StdIO) call chkabort and should therefore  *)
  141. (* not be called in critical sections of your program unless youve disabled   *)
  142. (* break checking by installing your own break handler.                  *)
  143.  
  144. PROCEDURE expand_args(     argcIn  : LONGINT ;
  145.                            argvIn  : ADDRESS ;
  146.                        VAR argvOut : LONGINT ;
  147.                        VAR argcOut : ADDRESS ) : LONGINT ;
  148.  
  149. (* This function will expand any amigaDOS wildcards that were specified on    *)
  150. (* the command line, A typical call might be:                      *)
  151. (*  err := StdLib.expand_args( M2Lib.argc, M2Lib.argc, M2Lib.argc, M2Lib.argv)*)
  152. (* Return 0 on success, non-zero otherwise (run out of stack etc)             *)
  153. (* Any program that uses expand_args() should be run with at least 8K of stack*)
  154.  
  155. END StdLib.
  156.  
  157.